//
//  SSRegisterNextView.m
//  textDemo
//
//  Created by Esther on 2017/2/18.
//  Copyright © 2017年 Esther. All rights reserved.
//

#import "SSRegisterNextView.h"


@interface SSRegisterNextView()<UITextFieldDelegate>
@property(nonatomic, strong) UILabel *tostLabel;
@property(nonatomic, strong) UILabel *backLabel;
@property(nonatomic, strong) UITextField *codeTextField;
@property(nonatomic, strong) UIButton *timeButton;
@property(nonatomic, strong) UILabel *lineLabel;
@property(nonatomic, strong) UIButton *registerButton;

@end
@implementation SSRegisterNextView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
       
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self addSubview:self.tostLabel];
    [self addSubview:self.backLabel];
    [self addSubview:self.codeTextField];
    [self addSubview:self.timeButton];
    [self addSubview:self.lineLabel];
    [self addSubview:self.registerButton];
    
#pragma mark - 约束
    
    __weak typeof (self) weakSelf = self;
    [_tostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(35);
        make.top.right.equalTo(weakSelf);
        make.left.equalTo(weakSelf.mas_left).offset(15);
    }];
    
    [_backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
    }];
    
    [_codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.backLabel.mas_left).offset(15);
        make.top.bottom.equalTo(weakSelf.backLabel);
        make.right.equalTo(weakSelf.backLabel.mas_right).offset([UIScreen ss_screenWidth] - 110);
        
    }];
    
    [_timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 44));
        make.right.top.equalTo(weakSelf.backLabel);
    }];
    
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(1, 30));
        make.centerY.equalTo(weakSelf.backLabel.mas_centerY);
        make.right.equalTo(weakSelf.timeButton.mas_left).offset(-1);
    }];
    
    [_registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.height.mas_equalTo(35);
        make.top.equalTo(weakSelf.codeTextField.mas_bottom).offset(15);
    }];

    
}
#pragma mark -
#pragma mark - 监听方法
- (void)clickRegisterButton{
    
    if (self.registerBlock) {
        self.registerBlock(self.codeTextField.text);
    }
    
}
- (void)codeTextFieldChange:(UITextField *)textField{
    if (textField.text.length == 6) {
        self.registerButton.userInteractionEnabled = YES;
        self.registerButton.backgroundColor = [UIColor ss_colorWithRed:56 green:166 blue:241];
        
    }else{
        self.registerButton.backgroundColor= [UIColor ss_colorWithRed:229 green:229 blue:229];
        self.registerButton.userInteractionEnabled = NO;
    }
    
}

- (void)clickButtonCodeSendAgain{
    NSLog(@"clickButtonCodeSendAgain");
}
#pragma mark -
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (range.location == 6) {
        return NO;
    }
    return YES;
}

#pragma mark -
#pragma mark - 倒计时
- (void)GCDTime{
    __block NSInteger time = 10;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (time < 1) {
            // 取消倒时
            dispatch_source_cancel(timer);
                NSMutableAttributedString *string4 = [[NSMutableAttributedString alloc] initWithString:@"重新发送" attributes:@{NSForegroundColorAttributeName : [UIColor ss_colorWithRed:56 green:166 blue:243]}];
            self.timeButton.userInteractionEnabled = YES;
            // 切换线程
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.timeButton setAttributedTitle:string4 forState:(UIControlStateNormal)];
            });
            
            
        }else{
            // 切换线程
            dispatch_async(dispatch_get_main_queue(), ^{
                self.timeButton.userInteractionEnabled = NO;
                // 富文本
                [self.timeButton setAttributedTitle:[self makeTimeLabelAttributed:time] forState:(UIControlStateNormal)];
            });
            
            time --;
        }

    });
    dispatch_resume(timer);
}

#pragma mark -
#pragma mark - 富文本
- (NSMutableAttributedString *)makeTostLabelAttributed:(NSString *)phoneNumString{
    NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:@"验证码已经发送到" attributes:@{NSForegroundColorAttributeName : [UIColor ss_colorWithRed:139 green:139 blue:139]}];
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"+ 86 %@",phoneNumString] attributes:@{NSForegroundColorAttributeName : [UIColor ss_colorWithRed:56 green:166 blue:243]}];
    [string1 insertAttributedString:string2 atIndex:string1.length];
    return string1;
    
}
- (NSMutableAttributedString *)makeTimeLabelAttributed:(NSInteger)time{
    
    NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%li",time] attributes:@{NSForegroundColorAttributeName : [UIColor ss_colorWithRed:56 green:166 blue:243]}];
    NSMutableAttributedString *string4 = [[NSMutableAttributedString alloc] initWithString:@"秒后重试" attributes:@{NSForegroundColorAttributeName : [UIColor ss_colorWithRed:139 green:139 blue:139]}];
    [string3 insertAttributedString:string4 atIndex:string3.length];
    return string3;
}

#pragma mark -
#pragma mark - set赋值
- (void)setPhoneNumString:(NSString *)phoneNumString{
    _phoneNumString = phoneNumString;
    
    // 富文本
    self.tostLabel.attributedText = [self makeTostLabelAttributed:phoneNumString];
    
    
}

#pragma mark -
#pragma mark - 懒加载
- (UILabel *)tostLabel{
    if (!_tostLabel) {
        _tostLabel = [[UILabel alloc] init];
        _tostLabel.text = @"验证码已经发送到+ 86";
        _tostLabel.font = [UIFont systemFontOfSize:12.0];
    }
    return _tostLabel;
}

- (UILabel *)backLabel{
    if (!_backLabel) {
        _backLabel = [[UILabel alloc] init];
        _backLabel.backgroundColor = [UIColor whiteColor];
        _backLabel.layer.borderWidth = 1;
        _backLabel.layer.borderColor = [UIColor ss_colorWithRed:188 green:188 blue:188].CGColor;
    }
    return _backLabel;
}

- (UITextField *)codeTextField{
    if (!_codeTextField) {
        _codeTextField = [[UITextField alloc] init];
        _codeTextField.delegate = self;
        _codeTextField.placeholder = @"请输入验证码";
        [_codeTextField addTarget:self action:@selector(codeTextFieldChange:) forControlEvents:UIControlEventAllEditingEvents];
        
        
    }
    return _codeTextField;
}

- (UIButton *)timeButton{
    if (!_timeButton) {
        _timeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_timeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _timeButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
//        self.timeButton.userInteractionEnabled = NO;
        [_timeButton setTitle:@"验证码失败" forState:(UIControlStateNormal)];
        
        [_timeButton addTarget:self action:@selector(clickButtonCodeSendAgain) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _timeButton;
}

- (UILabel *)lineLabel{
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc] init];
        _lineLabel.backgroundColor = [UIColor ss_colorWithRed:188 green:188 blue:188];
    }
    return _lineLabel;
}

- (UIButton *)registerButton{
    if (!_registerButton) {
        _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        _registerButton.backgroundColor = [UIColor ss_colorWithRed:229 green:229 blue:229];
        _registerButton.userInteractionEnabled = NO;
        [_registerButton setTitleColor:[UIColor ss_colorWithRed:132 green:132 blue:132] forState:UIControlStateNormal];
        [_registerButton addTarget:self action:@selector(clickRegisterButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerButton;
}


@end
